/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年7月15日 下午11:02:06 
 * 
 */
package phoenics.upservice.restcontroller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import phoenics.alipay.AlipayConfig;
import phoenics.common.BaseData.RestResp;
import phoenics.common.BaseData.RestRespNoWarp;
import phoenics.manpads.common.annotation.NoWrapper;
import phoenics.upservice.entity.User;
import phoenics.upservice.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 */
@RestController
@RequestMapping("/pay")
public class AppSt {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AppSt.class);
	@Autowired
	UserService userService;
	@Value("${pay.alipay.notify.url}")
	private String ALIPAY_NOTIFY_URL;
	@GetMapping(value = "/sign")
	public RestResp takesignData(@RequestParam(value = "pid", defaultValue = "") String pid,
								 @RequestParam(value = "ordreMoney", defaultValue = "0.01") String ordreMoney) throws AlipayApiException {

		//实例化客户端
		AlipayClient alipayClient = new DefaultAlipayClient(
				AlipayConfig.gateway,
				AlipayConfig.partner,
				AlipayConfig.private_key,
				AlipayConfig.format,
				AlipayConfig.input_charset,
				AlipayConfig.ali_public_key,
				AlipayConfig.sign_type);
		//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify
		//AlipayOpenPublicTemplateMessageIndustryModifyRequest alipay_request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
		//AlipayTradeQueryRequest alipay_request = new AlipayTradeQueryRequest();
		AlipayTradeAppPayRequest alipay_request = new AlipayTradeAppPayRequest();
		User u=userService.takePrincipal();
		Date now=new Date();
		String orderId=u.getId()+"_"+pid+"_"+now.getTime();
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody(pid);
		model.setSubject("订单提交");
		model.setOutTradeNo(orderId);
		model.setTimeoutExpress("30m");
		model.setTotalAmount(ordreMoney);
		model.setProductCode("QUICK_MSECURITY_PAY");
		alipay_request.setBizModel(model);
		alipay_request.setNotifyUrl(ALIPAY_NOTIFY_URL);
		//alipay_request.setNotifyUrl("商户外网可以访问的异步地址");
		AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alipay_request);
		//调用成功，则处理业务逻辑
		if(response.isSuccess()){
			String order = response.getBody();
			logger.info(order);
			return RestResp.success(order);
		}
		return RestResp.fail();
	}
	//@PostMapping(value = "/alipayNotify")

}
